package com.ryoma.Utils;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

import javax.xml.namespace.QName;

public class WebServicesRpcUtils {

	/**
	 * 
	 * @param serviceUrl  RPC webservice接口地址
	 * @param namespace   接口命名空间
	 * @param functionname 方法名称
	 * @param object 传入参数
	 * @return 接口返回值
	 */
	public static String rpcwebservice(String serviceUrl, String namespace, String functionname, Object[] object){
		Object resultObject = null;
		String retruntxt = "";
		try {
			// 使用RPC方式调用WebService
			RPCServiceClient serviceClient = new RPCServiceClient();
			Options options = serviceClient.getOptions();
			options.setAction("sendMsg");
			// 指定调用WebService的URL
			EndpointReference targetEPR = new EndpointReference(serviceUrl);
			options.setTo(targetEPR);
			// 指定方法的参数值
			Object[] opAddEntryArgs = null;
			opAddEntryArgs = object;// phonenumber
			// 指定方法返回值的数据类型的Class对象
			@SuppressWarnings("rawtypes")
			Class[] classes = new Class[] { String.class };
			// 指定要调用的方法及WSDL文件的命名空间
			QName opAddEntry = new QName(namespace, functionname);
			// 调用方法并输出该方法的返回值
			// System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,
			// classes)[0]);
			resultObject = serviceClient.invokeBlocking(opAddEntry,
					opAddEntryArgs, classes)[0];
			retruntxt = resultObject.toString();
		}catch (AxisFault e) {
			e.printStackTrace();
			retruntxt= "";
		}
		return retruntxt;
	}
	
	/*public static String sendMsg2AppInformation(Gissourcedilatation entity,String requestformId){
		Gisifcservicelogs serlogs=new Gisifcservicelogs();
		User user=UserUtils.getUser();
		serlogs=WebServicesRpcUtils.getGisifcservicelogsbyuser(user, serlogs);
		serlogs.setServicetype("SIMPLEEXPAND_INNET");
		serlogs.setCreateDate(new Date());
		String result = "";
		String[] labels = SENDMSG2APPINFORMATION.split(",");//xml标签
		StringBuffer xml = new StringBuffer();
		xml.append("<?xml version='1.0' encoding='utf-8'?>");
		xml.append("<opDetail><recordInfo>");
		for(int i = 0;i<labels.length;i++){
			String[] label = labels[i].split("!!");
			System.out.println(label[1]);
			Object value = Reflections.invokeGetter(entity,label[2]);
			if(value==null&&entity.getDataSource().equals("1")){//仅资管预警
				result = "该条数据的【"+label[0]+"】查询失败！无法推送至装维！请确认数据是否正确！！";
				logger.error("sendMsg2AppInformation:"+entity.getId()+","+result);
				return result;
			}
			xml.append("<fieldInfo>");
			xml.append("<fieldChName>");
			xml.append(label[0]);
			xml.append("</fieldChName>");
			xml.append("<fieldEnName>");
			xml.append(label[1]);
			xml.append("</fieldEnName>");
			xml.append("<fieldContent>");
			if(label[1].equals("areaCode")){
				xml.append(value.toString().trim().split("市")[0]);
			}else if(label[1].toString().equals("flow_id")){
				xml.append(requestformId);
			}else if(label[1].toString().equals("dealRemark")&&entity.getDataSource().equals("2")){
				xml.append("资管预警");
			}
			else{
				xml.append(value==null?"":value);
			}
			
			xml.append("</fieldContent>");
			xml.append("</fieldInfo>");
		}
		xml.append("</recordInfo></opDetail>");
		logger.info("sendMsg2AppInformation -- 推送至装维报文:"+xml.toString());
		serlogs.setXml(xml.toString());
		result = rpcwebservice(APP_BUILD_URL, SENDMSG2APPINFORMATIONNAMESPACE, SENDMSG2APPINFORMATIONFUNCTIONNAME, new Object[] {xml.toString()});
		String values = "";
		logger.info("sendMsg2AppInformation -- 装维反馈报文:"+result.toString());
		if(result.contains("<fieldEnName>resultCode</fieldEnName>")){
			String[] logs = result.split("<fieldEnName>resultCode</fieldEnName>");
			values = (logs[1].split("<fieldContent>")[1]).split("</fieldContent>")[0];
			if(values.equals("0")){
				serlogs.setMessage(result.toString());
				serlogs.setReturnmessage("成功");
				serlogs.setReturntype("0");
				result = "true";
			}else{
				serlogs.setMessage(result.toString());
				serlogs.setReturnmessage("失败");
				serlogs.setReturntype("1");
				result = "装维接口接收失败！！！";
			}
		}else{
			result = "false";
		}
		ifcservicelogsService.save(serlogs);
		return result;
	}*/
	
	
	public static String buildXML(String xmlbody, String xmlLabel, String xmlValue){
		StringBuffer sb = new StringBuffer();
		sb.append("<![CDATA[<?xml version='1.0' encoding='GBK'?>");
		String[] bodys = xmlbody!=null?xmlbody.split(","):new String[]{};
		String[] lables = xmlLabel!=null?xmlLabel.split(","):new String[]{};
		String[] xmlValues = xmlValue!=null?xmlValue.split(","):new String[]{};
		
		String end = "";
		if(xmlValues.length>0){
			if(bodys.length>0){
				for(String body:bodys){
					sb.append("<"+body+">");
					end = end + "</"+body+">";
				}
			}else{
				return "xml外层Body标签出错！请检查！";
			}
			if(lables.length>0){
				for(int i = 0 ;i<lables.length;i++){
					String[] lb = lables[i].split(" ");//有部分报文要求格式为 <param type="String> </param>
					sb.append("<"+lables[i]+">").append(xmlValues[i]).append("</"+lb[0]+">");
				}
			}else{
				return "xml value部分标签错误！请检查！";
			}
		} else {
			return " xml value部分值为空！请检查！";
		}
		sb.append(end.trim()).append("]]>");
		return sb.toString().trim();
	}
}
